
#ifndef _MEMBER_BTERRAIN_H_
#define _MEMBER_BTERRAIN_H_

#include <BQuadTree>
#include <BTerrain>
#include "ITriangleCallback.h"

#define terrain_member(terrain)		((member_BTerrain*)((BTerrain*)terrain)->_ptr)

namespace BWE
{
	typedef BQuadTree<BIntArray>				QuadTree;
	typedef BQuadTree<BIntArray>::Item		QuadTreeItem;
	typedef BQuadTree<BIntArray>::ItemArray	QuadTreeItemArray;

	class member_BTerrain
	{
		friend class BTerrain;

		struct Node
		{
			BSpace	space;
			BVector	center;
			int		index = -1;
		};
		typedef BArray<Node> NodeArray;

	public:
		member_BTerrain(BTerrain* terrain);
		~member_BTerrain();

		bool rebuild();

		void freshQuadTree();

		void buildTree(int startIndex, int endIndex);
		void overlapping(ITriangleCallback* callback, const BSpace& space);
		void overlapping(ITriangleCallback* callback, const BLine& line);
		void processNode(ITriangleCallback* callback, int index);

		BTerrain*				boss;
		Align					align;
		BReal					width;
		BReal					height;
		BReal					skirtHeight;
		BSpace					localSpace;
		BRealTable				elevation;
		bool					dirtyArea;
		bool					dirtyZone;
		bool					dirtySkirt;
		BHolder<BVectorArray>	vertices;
		BHolder<BVectorArray>	normals;
		BHolder<BColorArray>	colors;
		BHolder<BCoordArray>	coords;
		BHolder<BIntArray>	indices;
		BHolder<QuadTree>		quadTree;
		bool					dirtyTree;
		int						lastIndex;
		NodeArray				leafNodes;
		NodeArray				treeNodes;
		int						debugIndex;

	};
}

#endif